package com.hnevc.androidhomework4;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    EditText mEtName,mEtPassword;
    Button mBtnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEtPassword = (EditText) findViewById(R.id.et_password);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mEtName = (EditText) findViewById(R.id.et_name);
       mBtnLogin.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               String name =mEtName.getText().toString();
               String password = mEtPassword.getText().toString();

               String n1="admin";
               String n2="123";
               if (name.equals(n1) && password.equals(n2)){
                   Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                   intent .putExtra("name",name);
                   startActivity(intent);


               }else{
                   Toast.makeText(MainActivity.this,"用户名错误或密码错误",Toast.LENGTH_SHORT).show();
               }
           }


       });
    }

    private void saveUserNamePassword(String name,String password) {
        try {

            String namePassword = name+" "+password;
            FileOutputStream fileOutputStream = openFileOutput("user_password.txt", MODE_PRIVATE);
            byte[] bytes = namePassword.getBytes();
            fileOutputStream.write(bytes);
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
